<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root">

    </div>
</body>
<script src="../react@17.js"></script>
<script src="../react-dom@17.js"></script>
<script src="../babel.js"></script>
<script type="text/babel">
    class App extends React.Component{
        state={
            list:[
                {
                    id:1,
                    name:"奔驰",
                    date:new Date()
                },{
                    id:2,
                    name:"宝马",
                    date:new Date()
                },{
                    id:3,
                    name:"法拉利",
                    date:new Date()
                },{
                    id:4,
                    name:"劳斯莱斯",
                    date:new Date()
                }
            ],
            name:"",
            keyWords:""
        }
        render(){
            let {list,name,keyWords} = this.state;
            return (
                <div>
                    <div>
                        id：<input type="text" disabled={true} />    
                        Name: <input type="text" value={name} onChange={(e)=>this.changeName(e)} onKeyDown={(e)=>this.keyDown(e)}/> <button onClick={()=>this.Add()}>添加</button>
                        搜索名称关键字： <input type="text" value={keyWords} onChange={(e)=>this.Search(e)}/>
                    </div>
                    <div>
                        <table border='1'>
                         <thead>
                            <tr>
                                <th>id</th>
                                <th>Name</th>
                                <th>Ctime</th>
                                <th>操作</th>
                            </tr>    
                        </thead> 
                        <tbody>
                            {
                                // includes 判断当前字符串中是否存在某个字符若存在则返回true
                                list.filter((item)=>item.name.includes(keyWords)).map((item,index)=>{
                                    return (
                                        <tr key={index}>
                                            <td>{item.id}</td>
                                            <td>{item.name}</td>
                                            <td>{(item.date).toLocaleDateString()}</td>
                                            <td>
                                                <button onClick={()=>this.Remove(item.id)}>删除</button>
                                            </td>    
                                        </tr>
                                    )
                                })
                            }    
                        </tbody>   
                        </table>   
                    </div>
                </div>
            )
        }
        changeName(e){
            this.setState({
                name:e.target.value
            })
        }
        Add(){
            let name = this.state.name;
            console.log(name)
            let id = this.state.list[this.state.list.length-1].id +1;
            let obj = {
                name,
                id,
                date:new Date()
            }
            this.state.list.push(obj);
            this.setState({
                list:this.state.list,
                name:""
            })
        }
        Search(e){
            let keyWords = e.target.value;
            this.setState(()=>{
                return {
                    keyWords
                }
            })
        }
        Remove(id){
            let {list} = this.state;
            // 先根据当前要删除的id找到对应索引
            let index = list.findIndex((item)=>id===item.id);
            if(index!=-1){
                list.splice(index,1);
                this.setState({
                    list:list
                })
            }
        }
        keyDown(e){
            if(e.keyCode===13){
                this.Add();
            }
        }
    }
    ReactDOM.render(<App/>,document.getElementById("root"))
</script>
</html>